import 'package:bruno/bruno.dart';

class DataFormatUtils {
  /// 格式化级联选择所需数据结构
  static BrnPickerEntity brnPickerEntityMap(List list, PickerFilterType type) {
    List<BrnPickerEntity> entitys = []
      ..addAll(
        list.map((menuMap) {
          _brnPickerEntityMapFormat(menuMap, type);

          return BrnPickerEntity.fromMap(menuMap);
        }),
      );

    BrnPickerEntity topMenu = BrnPickerEntity()..children = entitys;

    if (entitys.length > 0) {
      entitys.forEach((entity) => entity.configChild());
      entitys.forEach((entity) => entity.parent = topMenu);
    }

    return topMenu;
  }

  // 格式化树形数据，增加所需字段
  static void _brnPickerEntityMapFormat(
    Map<String, dynamic> map,
    PickerFilterType type,
  ) {
    map['id'] = '${map['id']}';
    map['key'] = '${map['id']}';
    map['name'] = map['menuName'] ?? map['deptName'];
    map['type'] = type.name;

    List? children = map['children'];
    if (children != null) {
      children.forEach((entity) => _brnPickerEntityMapFormat(entity, type));
    }
  }
}
